import { Body, Controller, Get, HttpCode, Post, Req, UseFilters, UseGuards, UseInterceptors, UsePipes, ValidationPipe } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { Crud, CrudController } from '@nestjsx/crud';
import { AuthService } from 'libs/auth';
import { HandleStatus } from 'libs/db/entity/enum/handle-status.enum';
import { Villager } from 'libs/db/entity/villager.entity';
import { LoggerInfo, moduleLoggerInfo } from '../_decorator/logger-info';
import { HttpExceptionFilter } from '../_filter/http-exception.filter';
import { LoggingInterceptor } from '../_interceptor/logging.interceptor';
import { LoginVillager } from './dto/login-villager.dto';
import { VillagerCrudOps } from './Villager-crud-ops';
import { VillagerService } from './Villager.service';


@Crud(VillagerCrudOps)
@UseFilters(HttpExceptionFilter)
@UsePipes(ValidationPipe)
@UseInterceptors(LoggingInterceptor)
@ApiTags('信息管理 - 村民')   //标签
@Controller('villager')
export class VillagerController implements CrudController<Villager> {
  constructor(
    public service: VillagerService,
    private readonly authService: AuthService,
  ) {
  }

  @UseGuards(AuthGuard('local-web'))
  @HttpCode(200)
  @LoggerInfo(new moduleLoggerInfo ('用户登录查询', HandleStatus.LOG))
  @Post('/login')
  login(@Body() user: LoginVillager, @Req() req : Request | any) {
    return this.authService.login(req.user)
  }

  //查询 个人信息
  @UseGuards(AuthGuard('jwt-web'))
  @ApiBearerAuth()
  @LoggerInfo(new moduleLoggerInfo ('个人信息查询', HandleStatus.LOG))
  @Get('info')
  getMyInfo(@Req() req) {
    return req.user;
  }
}
